<template>
  <div class="myTable">
    <div class="header">
      <div class="title">{{ title }}</div>
      <div class="handler">
        <slot name="header-handler"></slot>
      </div>
    </div>
    <el-table
      :data="tableData"
      border
      stripe
      style="width: 100%"
      @selection-change="selectionChange"
      v-bind="childrenProps"
    >
      <el-table-column
        v-if="showSelectColumn"
        type="selection"
        align="center"
        width="60"
      ></el-table-column>
      <el-table-column
        v-if="showIndexColumn"
        type="index"
        label="序号"
        align="center"
        width="60"
      ></el-table-column>
      <template v-for="item in propList" :key="item.prop">
        <el-table-column v-bind="item" align="center">
          <!-- 动态插槽 -->
          <template #default="scope">
            <slot :name="(item as any).prop" :row="scope.row">{{
              scope.row[(item as any).prop]
            }}</slot>
          </template>
        </el-table-column>
      </template>
    </el-table>
    <div class="footer" v-if="showFooter">
      <slot name="footer">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="page.current_page"
          :page-size="page.per_page"
          :page-sizes="[5, 10, 20, 50, 100]"
          :total="total"
          layout="total, sizes, prev, pager, next, jumper"
        />
      </slot>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent } from "vue"

export default defineComponent({
  props: {
    tableData: {
      type: Array,
      required: true
    },
    propList: {
      type: Array
      // required: true  // 报错
    },
    total: {
      type: Number,
      default: 0
    },
    page: {
      type: Object,
      default: () => ({ per_page: 20, current_page: 1 })
    },
    showIndexColumn: {
      type: Boolean,
      default: false
    },
    showSelectColumn: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ""
    },
    childrenProps: {
      type: Object,
      default: () => ({})
    },
    showFooter: {
      type: Boolean,
      default: true
    }
  },
  components: {},
  emits: ["selectionChange", "update:page"],
  setup(props, { emit }) {
    const selectionChange = (value: any) => {
      emit("selectionChange", value)
    }
    const handleSizeChange = (per_page: number) => {
      emit("update:page", { ...props.page, per_page })
    }
    const handleCurrentChange = (current_page: number) => {
      console.log(current_page)
      emit("update:page", { ...props.page, current_page })
    }
    return {
      selectionChange,
      handleSizeChange,
      handleCurrentChange
    }
  }
})
</script>

<style scoped lang="less">
.header {
  display: flex;
  height: 45px;
  padding: 0 5px;
  justify-content: space-between;
  align-items: center;
  .title {
    font-size: 20px;
    font-weight: 700;
  }
  .handler {
    align-items: center;
  }
}
.footer {
  margin-top: 15px;
  .el-pagination {
    text-align: right;
  }
}
</style>
